import { fileURLToPath, URL } from 'node:url'
import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import vueDevTools from 'vite-plugin-vue-devtools'
import components from 'unplugin-vue-components/vite'
import { AntDesignXVueResolver } from 'ant-design-x-vue/resolver'

// https://vite.dev/config/
export default defineConfig(({ mode }) => {
  // 加载环境变量
  const env = loadEnv(mode, process.cwd())

  // 根据环境确定API基础URL
  const API_BASE_URL = env.VITE_API_BASE_URL || 'http://127.0.0.1:9998'

  return {
    plugins: [
      vue(),
      vueJsx(),
      vueDevTools(),
      components({
        resolvers: [AntDesignXVueResolver()],
      }),
    ],
    resolve: {
      alias: {
        '@': fileURLToPath(new URL('./src', import.meta.url)),
      },
    },
    server: {
      proxy: {
        '/ai-api': {
          target: API_BASE_URL,
          changeOrigin: true,
          rewrite: (path) => path,
        },
        '/user-api': {
          target: API_BASE_URL,
          changeOrigin: true,
          rewrite: (path) => path,
        },
        '/vip-api': {
          target: API_BASE_URL,
          changeOrigin: true,
          rewrite: (path) => path,
        },
        '/pay-api': {
          target: API_BASE_URL,
          changeOrigin: true,
          rewrite: (path) => path
        },
        '/knowledge-api': {
          target: API_BASE_URL,
          changeOrigin: true,
          rewrite: (path) => path
        }
      },
    },
  }
})
